package com.twu.biblioteca.views;

import com.twu.biblioteca.config.LibaryResourceType;
import com.twu.biblioteca.config.Message;
import com.twu.biblioteca.models.entities.Book;
import com.twu.biblioteca.models.services.Libary;
import com.twu.biblioteca.models.entities.LibaryResource;
import com.twu.biblioteca.models.entities.Movie;
import com.twu.biblioteca.utils.EnterUtil;

import java.lang.reflect.Method;
import java.util.List;

import static com.twu.biblioteca.config.LibaryResourceType.BOOK;
import static com.twu.biblioteca.config.LibaryResourceType.MOVIE;

public class LibaryView {

    public static void listBooks(List<Book> books) {
        String column1 = "| Name" + getProperBlank(getMaxFieldLength(BOOK, "Name"), "Name");
        String column2 = "| Author"+ getProperBlank(getMaxFieldLength(BOOK, "Author"), "Author");
        String column3 = "| Publish Date"+ getProperBlank(getMaxFieldLength(BOOK, "PublishDate"), "Publish Date") + "|\n";

        int totalLength = (column1 + column2 + column3).length();
        String bookListResult = "";
        bookListResult += hr(totalLength);
        bookListResult += (column1 + column2 + column3);
        bookListResult += hr(totalLength);
        for (Book book : books) {
            bookListResult += "| " + book.getName() + getProperBlank(getMaxFieldLength(BOOK, "Name"), book.getName());
            bookListResult += "| " + book.getAuthor()+ getProperBlank(getMaxFieldLength(BOOK, "Author"), book.getAuthor());
            bookListResult += "| " + book.getPublishDate()+ getProperBlank(getMaxFieldLength(BOOK, "PublishDate"), book.getPublishDate().toString()) + "|\n";
            bookListResult += hr(totalLength);
        }
        System.out.print(bookListResult);
    }

    public static String getEnteredBookName() {
        MessageView.showNotice(Message.ENTER_BOOK_NAME, false);
        return EnterUtil.getEnterdString();
    }

    public static String getEnteredMovieName() {
        MessageView.showNotice(Message.ENTER_MOVIE_NAME, false);
        return EnterUtil.getEnterdString();
    }

    public static void listMovies(List<Movie> movies) {
        String column1 = "| Name" + getProperBlank(getMaxFieldLength(MOVIE, "Name"), "Name");
        String column2 = "| Director"+ getProperBlank(getMaxFieldLength(MOVIE, "Director"), "Director");
        String column3 = "| Year"+ getProperBlank(getMaxFieldLength(MOVIE, "Year"), "Year");
        String column4 = "| Rate"+ getProperBlank(getMaxFieldLength(MOVIE, "Rate"), "Rate") + "|\n";

        int totalLength = (column1 + column2 + column3 + column4).length();
        String movieListResult = "";
        movieListResult += hr(totalLength);
        movieListResult += (column1 + column2 + column3 + column4);
        movieListResult += hr(totalLength);
        for (Movie movie : movies) {
            movieListResult += "| " + movie.getName() + getProperBlank(getMaxFieldLength(MOVIE, "Name"), movie.getName());
            movieListResult += "| " + movie.getDirector()+ getProperBlank(getMaxFieldLength(MOVIE, "Director"), movie.getDirector());
            movieListResult += "| " + movie.getYear()+ getProperBlank(getMaxFieldLength(MOVIE, "Year"), String.valueOf(movie.getYear()));
            movieListResult += "| " + movie.getRate()+ getProperBlank(getMaxFieldLength(MOVIE, "Rate"), movie.getRate().toString()) + "|\n";
            movieListResult += hr(totalLength);
        }
        System.out.print(movieListResult);
    }

    private static String hr(int totalLength) {
        String hrStr = "+";
        for (int i = 0; i < totalLength -3; i++) {
            hrStr += "-";
        }
        hrStr += "+\n";
        return hrStr;
    }

    private static String getProperBlank(int maxFeildLength, String feildValue) {
        int blankNumber = Math.abs(maxFeildLength - feildValue.length()) + 2;
        String blanks = "";
        for (int i=0; i< blankNumber; i++) {
            blanks += " ";
        }
        return blanks;
    }

    protected static int getMaxFieldLength(LibaryResourceType resourceType, String feild) {
        int maxNameLength = 0;
        List<? extends LibaryResource> resourceList = Libary.getLibaryResourceList(resourceType);
        Method method = null;
        try {
            method = resourceList.get(0).getClass().getMethod(getMethodName(feild));
            for (LibaryResource resource : resourceList) {
                int feildLength = method.invoke(resource).toString().length();
                if (feildLength > maxNameLength) {
                    maxNameLength = feildLength;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return maxNameLength;
    }

    private static String getMethodName(String feild) {
        return "get" + feild;
    }




}
